package com.jy.adscm.zltx.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.utils.AdscmSystemUtils;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.JyDrawingAuditing;
import com.jy.adscm.zltx.bean.JyDrawingEditInfo;
import com.jy.adscm.zltx.bean.JyDrawingList;
import com.jy.adscm.zltx.service.JyDrawingAuditingService;
import com.jy.adscm.zltx.service.JyDrawingEditInfoService;
import com.jy.adscm.zltx.service.JyDrawingListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 图纸修订记录 前端控制器
 * </p>
 *
 * @author zyl
 * @since 2018-10-26
 */
@RestController
@RequestMapping("/adscm/zltx/jyDrawingEditInfo")
public class JyDrawingEditInfoController {

    @Autowired
    JyDrawingEditInfoService jyDrawingEditInfoService;

    @Autowired
    JyDrawingListService jyDrawingListService;

    @Autowired
    JyDrawingAuditingService jyDrawingAuditingService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;

//    /**
//     * 查询最新的一个版本的图纸信息
//     * @param id
//     * @return
//     */
//    @RequestMapping("/selectHeadDrawingVersion/{id}")
//     public SimpleCoreResponse selectHeadDrawingVersion(@PathVariable Integer id) throws AdscmSystemException {
//
//        Wrapper<JyDrawingEditInfo> wrapper = new EntityWrapper<>();
//        wrapper.eq("drawing_id",id);
//
//        List<JyDrawingEditInfo> editInfos = jyDrawingEditInfoService.selectList(wrapper);
//
//
//        if (editInfos != null && editInfos.size()>0){
//
//            for (JyDrawingEditInfo editInfo : editInfos) {
//
//            }
//
//            JyDrawingList jyDrawingList = jyDrawingListService.selectJyDrawingById(id);
//
//
//        }else {
//            throw new AdscmSystemException("没有查询到ID【"+id+"】的图纸基本数据");
//        }
//
//
//
//        return new SimpleCoreResponse("200","查询成功" ,jyDrawingList);
//     }

    /**
     * 新增图纸修改记录
     * @param JyDrawingEditInfo
     * @param request
     * @return
     * @throws AdscmSystemException
     * @throws IOException
     */
    @RequestMapping("/insert")
     public SimpleCoreResponse insert(@RequestBody JyDrawingEditInfo JyDrawingEditInfo, HttpServletRequest request) throws AdscmSystemException, IOException {
//        ObjectMapper mapper = new ObjectMapper();
//        JyDrawingEditInfo JyDrawingEditInfo = mapper.readValue(JyDrawingEditInfoStr,JyDrawingEditInfo.class);


        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        JyDrawingEditInfo.setModifier(token.getUser().getId());
        JyDrawingEditInfo.setModifyTime(new Date());
        jyDrawingListService.updateJyDrawing(Integer.valueOf(String.valueOf(JyDrawingEditInfo.getDrawingId())));
        jyDrawingEditInfoService.addAuditingEditInfo(JyDrawingEditInfo);



        return new SimpleCoreResponse("200","新增成功" ,null);
     }
    @RequestMapping("/update")
    public SimpleCoreResponse update(String JyDrawingEditInfoStr, HttpServletRequest request) throws AdscmSystemException, IOException {

        ObjectMapper mapper = new ObjectMapper();
        JyDrawingEditInfo JyDrawingEditInfo = mapper.readValue(JyDrawingEditInfoStr,JyDrawingEditInfo.class);


        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        JyDrawingEditInfo.setModifier(token.getUser().getId());
        JyDrawingEditInfo.setModifyTime(new Date());
        jyDrawingListService.updateJyDrawing(Integer.valueOf(String.valueOf(JyDrawingEditInfo.getDrawingId())));
        return new SimpleCoreResponse("200","修改成功" ,jyDrawingEditInfoService.updateById(JyDrawingEditInfo));
    }


//    @RequestMapping("/auditingEditInfo")
//    public SimpleCoreResponse auditingEditInfo(String JyDrawingEditInfoStr, HttpServletRequest request) throws AdscmSystemException, IOException {
//
//        ObjectMapper mapper = new ObjectMapper();
//        JyDrawingEditInfo JyDrawingEditInfo = mapper.readValue(JyDrawingEditInfoStr,JyDrawingEditInfo.class);
//
//
//        String tokenId = request.getParameter("tokenId");
//        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
//        JyDrawingEditInfo.setApprovalPerson(token.getUser().getId());
//        JyDrawingEditInfo.setApprovalTime(new Date());
//
//        jyDrawingEditInfoService.auditingEditInfo(JyDrawingEditInfo);
//
//        return new SimpleCoreResponse("200","审核成功" ,null);
//    }

    /**
     *  审核图纸操作
     * @param jyDrawingAuditing
     * @param request
     * @return
     * @throws AdscmSystemException
     * @throws IOException
     */
    @RequestMapping("/auditingEditInfo")
    public SimpleCoreResponse auditingEditInfo(@RequestBody JyDrawingAuditing jyDrawingAuditing, HttpServletRequest request) throws AdscmSystemException, IOException {

        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);

        jyDrawingAuditingService.auditingEditAudit(jyDrawingAuditing , token.getUser());

        return new SimpleCoreResponse("200","审核成功" ,null);
    }
    /**
     *  发布图纸修订记录
     * @param id
     * @param request
     * @return
     * @throws AdscmSystemException
     * @throws IOException
     */
    @RequestMapping("/releaseEditInfo/{id}")
    public SimpleCoreResponse releaseEditInfo(@PathVariable Long id, HttpServletRequest request) throws AdscmSystemException, IOException {

        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);

        jyDrawingEditInfoService.releaseEditInfo(id,token.getUser());

        return new SimpleCoreResponse("200","发布成功" ,null);
    }

    @RequestMapping("/delete/{id}")
    public SimpleCoreResponse update(@PathVariable Integer id, HttpServletRequest request){
        return new SimpleCoreResponse("200","新增成功" ,jyDrawingEditInfoService.deleteById(id));
    }
    @RequestMapping("/selectById/{id}")
    public SimpleCoreResponse selectById(@PathVariable Integer id, HttpServletRequest request){
        JyDrawingEditInfo jyDrawingEditInfo = jyDrawingEditInfoService.selectById(id);
        return new SimpleCoreResponse("200","新增成功" ,jyDrawingEditInfo);
    }
    @RequestMapping("/selectByDrawingId/{id}")
    public SimpleCoreResponse selectByDrawingId(@PathVariable Long id, HttpServletRequest request) throws AdscmSystemException{

        return new SimpleCoreResponse("200","查询成功" ,jyDrawingEditInfoService.selectByDrawingId(id));
    }

//    /**
//     * 获取上一条或者下一条记录
//     * @param id
//     * @param type ：next:下一条，last:上一条
//     * @param request
//     * @return
//     * @throws AdscmSystemException
//     */
//    @RequestMapping("/selectLastOrNext/{id}/{type}")
//    public SimpleCoreResponse selectLastOrNext(@PathVariable Long id,@PathVariable String type, HttpServletRequest request) throws AdscmSystemException{
//
//        return new SimpleCoreResponse("200","查询成功" ,jyDrawingEditInfoService.selectLastOrNext(id,type));
//    }
    /**
     * 查看上一个版本的PDF，如果没有上一个版本，则看正式版
     * @param id
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/selectLastPdf/{id}")
    public void selectLastPdf(@PathVariable Long id,HttpServletRequest request, HttpServletResponse response) throws AdscmSystemException{

        String filePath = jyDrawingEditInfoService.selectLastVersion(id);

        AdscmSystemUtils.getInstance().writeFile(filePath,response);

    }
    /**
     * 下载上一个版本的PDF，如果没有上一个版本，则下载正式版
     * @param id
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/downLoadLastPdf/{id}")
    public void downLoadLastPdf(@PathVariable Long id,HttpServletRequest request, HttpServletResponse response) throws AdscmSystemException{

        String filePath = jyDrawingEditInfoService.selectLastVersion(id);

        AdscmSystemUtils.getInstance().downloadFile(filePath,response);

    }
}

